package com.zygk.qybofficial.app;

import android.content.Context;
import android.os.Environment;
import android.support.multidex.MultiDex;
import android.util.Log;
import cn.jpush.android.api.JPushInterface;
import com.lzy.imagepicker.ImagePicker;
import com.mabeijianxi.smallvideorecord2.DeviceUtils;
import com.mabeijianxi.smallvideorecord2.JianXiCamera;
import com.yanzhenjie.nohttp.InitializationConfig;
import com.yanzhenjie.nohttp.Logger;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.OkHttpNetworkExecutor;
import com.zygk.library.util.GlideImageLoader;

import com.zygk.library.util.Util;
import java.io.File;
import org.litepal.LitePalApplication;

/**
 * Created by wikison on 2016/6/2.
 */
public class AppApplication extends LitePalApplication {

  private static final String TAG = AppApplication.class.getSimpleName();

  private static AppApplication instance;
  private static AppApplication mAppApplication;
  private static Context mContext;
  private static AppApplication _instance;
  private static ImagePicker imagePicker;

  public AppApplication() {
    _instance = this;
  }

  public static Context getContext() {
    return mContext;
  }

  public static ImagePicker getImagePicker() {
    return imagePicker;
  }

  /**
   * 获取Application
   */
  public static synchronized AppApplication getApp() {
    return mAppApplication;
  }

  public static AppApplication instance() {
    if (_instance == null) {
      throw new IllegalStateException("Application not init!!!");
    }
    return _instance;
  }

  @Override public void onCreate() {
    super.onCreate();
    mContext = this;
    mAppApplication = this;

    initPush();
    initImagePicker();
    initNoHttp();
    initSmallVideo();
  }

  @Override protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
  }

  public static void initSmallVideo() {
    // 设置拍摄视频缓存路径
    File dcim = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    if (DeviceUtils.isZte()) {
      if (dcim.exists()) {
        JianXiCamera.setVideoCachePath(dcim + "/qyb/");
      } else {
        JianXiCamera.setVideoCachePath(
            dcim.getPath().replace("/sdcard/", "/sdcard-ext/") + "/qyb/");
      }
    } else {
      JianXiCamera.setVideoCachePath(dcim + "/qyb/");
    }
    // 初始化拍摄，遇到问题可选择开启此标记，以方便生成日志
    JianXiCamera.initialize(false, null);
  }

  private void initPush() {
    try {
      Log.e(TAG, "===" + Util.brand);
      // 手机类型(0:极光 1:小米 2:华为 )
      //
      //switch (Util.getInstance().phoneType()) {
      //  case 0:
      JPushInterface.setDebugMode(true);
      JPushInterface.init(this);
      //    break;
      //  case 1:
      //    // 注册push服务，注册成功后会向DemoMessageReceiver发送广播
      //    // 可以从DemoMessageReceiver的onCommandResult方法中MiPushCommandMessage对象参数中获取注册信息
      //    if (shouldInit()) {
      //      MiPushClient.registerPush(this, Constants.Mi_APP_ID, Constants.Mi_APP_KEY);
      //    }
      //    break;
      //  case 2:
      //    HMSAgent.init(this);
      //    break;
      //}
    } catch (Exception e) {
      Log.e(TAG, e + "===" + e.getMessage());
    }
  }

  private void initImagePicker() {
    imagePicker = ImagePicker.getInstance();
    imagePicker.setImageLoader(new GlideImageLoader());   //设置图片加载器
  }

  private void initNoHttp() {
    InitializationConfig config = InitializationConfig.newBuilder(this)
        // 全局连接服务器超时时间，单位毫秒，默认10s。
        .connectionTimeout(20 * 1000)
        // 全局等待服务器响应超时时间，单位毫秒，默认10s。
        .readTimeout(20 * 1000).networkExecutor(new OkHttpNetworkExecutor()).build();

    NoHttp.initialize(config); // NoHttp默认初始化。
    Logger.setDebug(true); // 开启NoHttp调试模式。
    Logger.setTag("NoHttpSample"); // 设置NoHttp打印Log的TAG。
  }
}
